package com.action;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.Major;
import com.pojo.News;
import com.service.NewsService;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/news")
public class NewsAction {
    @Autowired
    private NewsService newsService;

    @Value("${upload.dir}")
    private String upload;

    @Value("${upload.tmp.dir}")
    private String tmp;

    /**
     * 分页查询新闻标题
     *
     * @param page
     * @param limit
     * @param key
     * @param tpid
     * @return
     */
    @RequestMapping("/types")
    public ResponseBean types(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer limit,
                              @RequestParam(required = false) String key,
                              @RequestParam(required = false) String tpid) {
        IPage iPage = newsService.types(page, limit, key, tpid);
        return new ResponseBean(0, iPage.getTotal(), "ok", iPage.getRecords());
    }

    /**
     * 根据id查询新闻
     *
     * @return
     */
    @RequestMapping("/getnewsById")
    public ResponseBean getnewsById(String nsid) {
        News news = newsService.getById(nsid);
        if (news != null) {
            return new ResponseBean(200, "查询成功", news);
        }
        return ResponseBean.error("查询失败");
    }

    /**
     * 添加新闻
     *
     * @param news
     * @return
     */
    @RequestMapping("/add_news")
    public ResponseBean addNews(@RequestBody News news) {
        if (StringUtil.isNull(news.getTpid())) {
            return ResponseBean.error("请选择新闻类型");
        }
        if (StringUtil.isNull(news.getNscontent())) {
            return ResponseBean.error("请填写新闻内容");
        }
        if (StringUtil.isNull(news.getNsimg())) {
            return ResponseBean.error("请选择图片");
        }
//        if (StringUtil.isNull(news.getNssubtitle())) {
//            return ResponseBean.error("请填写副标题");
//        }
        if (news.getNsstatus() == null) {
            return ResponseBean.error("请选择是否显示");
        }
        if(news.getNsstatus() == 0) {
            news.setShowindex(0);
            news.setStatus(0);
        }
        if (news.getShowindex() == null) {
            return ResponseBean.error("请选择是否显示首页");
        }
        if (news.getStatus() == null) {
            return ResponseBean.error("请选择是否热门");
        }
        news.setNstime(new Date());
        if (StringUtil.isNull(news.getNstitle())) {
            return ResponseBean.error("请填写标题");
        }
        News m = newsService.getOne(new QueryWrapper<News>().eq("nstitle", news.getNstitle()).last("limit 1"));
        if (m != null) {
            return ResponseBean.error("名字已经存在");
        }
        boolean res = newsService.save(news);
        FileUtil.copy(tmp + news.getNsimg(), upload, true);
        return res ? new ResponseBean(200, "成功", "") : ResponseBean.error("新增失败");
    }

    /**
     * 修改新闻
     * @param news
     * @return
     */
    @RequestMapping("/updae_news")
    public ResponseBean updateNews(@RequestBody News news) {
        if (StringUtil.isNull(news.getNsid())) {
            return ResponseBean.error("请提交需要修改新闻的id");
        }
        News old = newsService.getById(news.getNsid());
        if (StringUtil.isNull(news.getTpid())) {
            return ResponseBean.error("请选择新闻类型");
        }
        if (StringUtil.isNull(news.getNscontent())) {
            return ResponseBean.error("请填写新闻内容");
        }
        if (StringUtil.isNull(news.getNsimg())) {
            return ResponseBean.error("请选择图片");
        } else {
            if (old.getNsimg() == null || !old.getNsimg().equals(news.getNsimg())) {
                FileUtil.copy(tmp + news.getNsimg(), upload, true);
            }
        }
//        if (StringUtil.isNull(news.getNssubtitle())){
//            return ResponseBean.error("请填写副标题");
//        }
        if (news.getNsstatus() == null) {
            return ResponseBean.error("请填写是否显示");
        }
        if (news.getNsstatus() == 0 ){
            news.setStatus(0);
            news.setShowindex(0);
        }

        if (news.getShowindex() == null) {
            return ResponseBean.error("请填写是否轮播图显示位置");
        }
        if (news.getStatus() == null) {
            return ResponseBean.error("请填写是否热门");
        }
        news.setNstime(new Date());
        if (StringUtil.isNull((news.getNstitle()))) {
            return ResponseBean.error("请填写标题");
        }
        boolean res = newsService.updateById(news);
        return res ? ResponseBean.OK : ResponseBean.error("修改失败");
    }

}